const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLoadMore:true,
    page:1,
    last_page:0,
    dataList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
     this.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log('没有更多数据');
      wx.showToast({
        title: '没有更多数据',
        icon:'none',
        duration:1000
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  //跳转详细页面 
  toDetail(e){
    wx.navigateTo({
      url: '/mircoApp/audioBook/audiodBookInfo/audioBookInfo?id=' + e.currentTarget.dataset.id
    });
  },
  getData(){
    let json = {
      page:this.data.page
    }
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.book.get-list");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        let res = resdata.data;
        if (res.result == 1) {
          this.data.page = res.data.data.current_page
          this.data.last_page = res.data.data.last_page
          this.setData({
            dataList:res.data.data.data
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1000,
          });
        }
      },
      fail: (res) => {
        console.log(res);
      },
    });  
  },
  _getMoreData(){
    var that = this;
    let urlStr = app.getNetAddresss("plugin.audio-book.frontend.book.get-list");
    console.log(this.data.page,this.data.last_page);
    if (this.data.page >= this.data.last_page) {
      that.setData({
        isLoadMore: false
      });
      return;
    } else {
      that.setData({
        page: this.data.page + 1
      });
      app._getNetWork({
        url: urlStr,
        data: {
          page: that.data.page
        },
        success: function (res) {
          if (res.data.result == 1) {
            let _data = res.data.data.data.data;
            that.setData({
              isLoadMore: true,
              dataList: that.data.dataList.concat(_data)
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  }
})